package com.ms;

import com.alibaba.fastjson.JSON;
import com.ms.exmaple.orm.config.ApplicationConfig;
import com.ms.exmaple.orm.entity.User;
import com.ms.exmaple.orm.service.UserService;
import com.ms.orm.conditions.query.QueryWrapper;
import com.ms.orm.conditions.update.UpdateWrapper;
import com.ms.orm.dboperator.BaseEntityDao;
import com.ms.orm.meta.Page;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Hello world!
 */
public class BaseService_App2 {
    public static void main(String[] args) {
        try {
            ApplicationContext app = new AnnotationConfigApplicationContext(ApplicationConfig.class);
            UserService userService = (UserService) app.getBean("userService");
            /////////////  //////////////////////////////////////////////////////////////
            List<User> userList = userService.findAll();

            /////////////  //////////////////////////////////////////////////////////////
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.gt("id","5");
            //queryWrapper.select(new String[]{"id","name"});
            queryWrapper.select(new String[]{"name"});
            //queryWrapper.orderByAsc("id");
            queryWrapper.orderByDesc("id");

            List<User> queryWrapper_list =  userService.selectList(queryWrapper);
            System.err.println("queryWrapper_list = "+ JSON.toJSONString(queryWrapper_list));


            /////////////  //////////////////////////////////////////////////////////////
            Map<String,Object> delParam = new HashMap<>();
            delParam.put("name","～～");
            int delResult = userService.deleteByMap(delParam);
            System.err.println("delResult = "+delResult);


            /////////////  //////////////////////////////////////////////////////////////
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("name","哈哈");
            updateWrapper.eq("id","1");
            int delResult_2 = userService.delete(updateWrapper);
            System.err.println("delResult_2 = "+delResult_2);


            /////////////  //////////////////////////////////////////////////////////////
            List<Integer> ids = new ArrayList<>();
            ids.add(1);
            ids.add(28);
            int delResult2 = userService.deleteBatchIds(ids);
            System.err.println("delResult2 = "+delResult2);

            /////////////  /////////////////////////////////////////////////////////////////////////
            UpdateWrapper updateWrapper2 = new UpdateWrapper<>();
            updateWrapper2.eq("name","呜呜");

            User user = new User();
            user.setName("哭泣");
            int updateRes = userService.update(user,updateWrapper2);
            System.err.println("updateRes = "+updateRes);


            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper queryWrapper2 = new QueryWrapper();
            List<String> ids3 = new ArrayList<>();
            ids3.add("事务回滚测试3");
            ids3.add("哭泣");
            queryWrapper2.in("name",ids3);
            List<User> userList3 =  userService.selectList(queryWrapper2);
            System.err.println("userList3 = "+ JSON.toJSONString(userList3));


            /////////////  //////////////////////////////////////////////////////////////////
            List<Integer> selectBatchIds_ids = new ArrayList<>();
            selectBatchIds_ids.add(1);
            selectBatchIds_ids.add(6);
            selectBatchIds_ids.add(26);
            List<User> selectBatchIds_list =  userService.selectBatchIds(selectBatchIds_ids);
            System.err.println("selectBatchIds_list = "+ JSON.toJSONString(selectBatchIds_list));


            /////////////  //////////////////////////////////////////////////////////////////
            Map<String,Object> conditionMap = new HashMap();
            conditionMap.put("name","事务回滚测试3");
            List<User> conditionMap_list = userService.selectByMap(conditionMap);
            System.err.println("conditionMap_list = "+ JSON.toJSONString(conditionMap_list));

            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper selectOneQueryWrapper = new QueryWrapper();
            selectOneQueryWrapper.eq("name","ww");
            Object selectOneQueryWrapperOne_res =  userService.selectOne(selectOneQueryWrapper);
            System.err.println("selectOneQueryWrapperOne_res = "+ JSON.toJSONString(selectOneQueryWrapperOne_res));

            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper selectCountQueryWrapper = new QueryWrapper();
            selectCountQueryWrapper.eq("name","ww");
            Object selectCountQueryWrapper_res =  userService.selectCount(selectCountQueryWrapper);
            System.err.println("selectCountQueryWrapper_res = "+ JSON.toJSONString(selectCountQueryWrapper_res));


            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper selectMapsQueryWrapper = new QueryWrapper();
            selectMapsQueryWrapper.eq("name","ww");
            Object selectMapsQueryWrapper_res =  userService.selectMaps(selectMapsQueryWrapper);
            System.err.println("selectMapsQueryWrapper_res = "+ JSON.toJSONString(selectMapsQueryWrapper_res));

            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper selectOneColumnListWrapper = new QueryWrapper();
            selectOneColumnListWrapper.select("name");
            selectOneColumnListWrapper.like("name","事");
            Object selectOneColumnListWrapper_res =  userService.selectOneColumnList(selectOneColumnListWrapper);
            System.err.println("selectOneColumnListWrapper_res = "+ JSON.toJSONString(selectOneColumnListWrapper_res));


            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper selectPageWrapper = new QueryWrapper();
            //selectPageWrapper.select("name");
            //selectPageWrapper.like("name","事");
            Object selectPageWrapper_res =  userService.selectPage(new Page<>(2,2),selectPageWrapper);
            System.err.println("selectPageWrapper_res = "+ JSON.toJSONString(selectPageWrapper_res));


            /////////////  //////////////////////////////////////////////////////////////////
            QueryWrapper selectPageMapWrapper = new QueryWrapper();
            //selectPageWrapper.select("name");
            //selectPageWrapper.like("name","事");
            Object selectPageMapWrapper_res =  userService.selectMapsPage(new Page<>(1,30),selectPageMapWrapper);
            System.err.println("selectPageMapWrapper_res = "+ JSON.toJSONString(selectPageMapWrapper_res));



            /////////////  //////////////////////////////////////////////////////////////////
            List<User> batchSaveList = new ArrayList<>();
            User user1 = new User();
            user1.setName("Userbatch1");
            User user2 = new User();
            user2.setName("Userbatch2");
            User user3 = new User();
            user3.setName("Userbatch3");
            batchSaveList.add(user1);
            batchSaveList.add(user2);
            batchSaveList.add(user3);
            int[] batchSaveList_res =  userService.batchSave(batchSaveList);
            System.err.println("batchSaveList_res = "+ JSON.toJSONString(batchSaveList_res));




        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main_bak(String[] args) {
        try {
            ApplicationContext app = new AnnotationConfigApplicationContext(ApplicationConfig.class);
            BaseEntityDao baseEntityDao = app.getBean(BaseEntityDao.class);
            List<Object> userList = baseEntityDao.findAll(User.class);
            System.err.println("userList = "+ JSON.toJSONString(userList));

//            User user = (User) baseEntityDao.findById(User.class,1);
//            System.err.println("user = "+ JSON.toJSONString(user));
//
            UserService userService = (UserService) app.getBean("userService");

            User user = (User) userService.findById(1L);
            System.err.println("user = "+ JSON.toJSONString(user));
//            List<User> userList2 = userService.findAll();
//            System.err.println("userList2 = "+ JSON.toJSONString(userList2));

            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.select(new String[]{"id","name"});
//            userService.

            //insert(userService);

//            insert(baseEntityDao);
            //update(userService);
//            update(baseEntityDao);


        }catch (Exception e) {
            e.printStackTrace();
        }
    }


}
